package ui;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;

import resourse.ResourseMannager;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JRadioButton;

import org.dom4j.DocumentException;

import config.ClientConfig;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


/**
 * 
 * @author ganlinux
 * 
 * 
 */
public class ClientConfigPanel extends javax.swing.JPanel {
	//EIMDesktop desktop=null;
	String imageName=null;
	public ClientConfigPanel() {
		//desktop=EIMDesktop.getInstance();
		initComponents();
	}
	private void initComponents() {
		resetButton = new javax.swing.JButton();
		useButton = new javax.swing.JButton();
		useButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

			}
		});

		if(imageName==null){
			//imageName=desktop.getDeskTopConfig().getWallPaper();
			if(imageName==null){
				imageName="Bliss.jpg";
			}
		}
		ImageIcon icon=ResourseMannager.getImageIcon(imageName, 279, 194);
		String[] iamgeNames=ResourseMannager.getWallImageNames();

		resetButton.setText("\u53d6\u6d88");
		resetButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				resetButtonMouseClicked(evt);
			}
		});

		useButton.setText("\u5e94\u7528");
		useButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				useButtonMouseClicked(evt);
			}
		});

		//创建ButtonGroup对象，不然就可以多选了，我们要的时单选
		ButtonGroup bg=new ButtonGroup();

		label = new JLabel("连接地址：");

		addrText = new JTextField();
		addrText.setColumns(10);

		label_1 = new JLabel("连接端口：");

		portText = new JTextField();
		portText.setColumns(10);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		layout.setHorizontalGroup(
			layout.createParallelGroup(Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
					.addGap(71)
					.addGroup(layout.createParallelGroup(Alignment.LEADING, false)
						.addComponent(label, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(label_1, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE))
					.addGroup(layout.createParallelGroup(Alignment.LEADING)
						.addGroup(layout.createSequentialGroup()
							.addGap(43)
							.addComponent(portText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(Alignment.TRAILING, layout.createSequentialGroup()
							.addGap(43)
							.addGroup(layout.createParallelGroup(Alignment.LEADING)
								.addComponent(addrText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addGroup(layout.createSequentialGroup()
									.addComponent(resetButton)
									.addGap(18)
									.addComponent(useButton)))))
					.addGap(95))
		);
		layout.setVerticalGroup(
			layout.createParallelGroup(Alignment.TRAILING)
				.addGroup(layout.createSequentialGroup()
					.addGap(90)
					.addGroup(layout.createParallelGroup(Alignment.BASELINE)
						.addComponent(label)
						.addComponent(addrText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(36)
					.addGroup(layout.createParallelGroup(Alignment.LEADING)
						.addGroup(layout.createSequentialGroup()
							.addComponent(portText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, 77, Short.MAX_VALUE)
							.addGroup(layout.createParallelGroup(Alignment.BASELINE)
								.addComponent(resetButton)
								.addComponent(useButton))
							.addGap(32))
						.addGroup(layout.createSequentialGroup()
							.addGap(3)
							.addComponent(label_1)
							.addContainerGap())))
		);
		this.setLayout(layout);
	}

	private void useButtonMouseClicked(java.awt.event.MouseEvent evt) {
		//String newName="wallimage/"+(String)(jComboBox1.getSelectedItem());
		//PropertyFrame.getInstance().setVisible(false);
		//EIMDesktop desktop=EIMDesktop.getInstance();
		//desktop.intWallPaper(newName);
		//DeskTopConfig deskTopConfig =desktop.getDeskTopConfig();
		//deskTopConfig.setWallPaper(newName);
		//ServerUtils.writeDeskTopConfig(deskTopConfig);
		//imageName=newName;
		//System.out.println(newName);
		
		ClientConfig clientConfig = ClientConfig.getInstance();
		
    	try {
    		clientConfig.changeConfig(addrText.getText(),portText.getText());
			JOptionPane.showMessageDialog(null, "配置已修改，重启服务器配置生效", "配置信息", JOptionPane.INFORMATION_MESSAGE);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void resetButtonMouseClicked(java.awt.event.MouseEvent evt) {
		//desktop.intWallPaper(imageName);
		//desktop.repaint();
		//PropertyFrame.getInstance().setVisible(false);
	}
	private javax.swing.JButton resetButton;
	private javax.swing.JButton useButton;
	private JLabel label;
	private JTextField addrText;
	private JLabel label_1;
	private JTextField portText;
}